package com.yuerlive.rainbow.module.mine;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.graphics.Paint;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yuerlive.rainbow.R;
import com.yuerlive.rainbow.base.BaseActivity;
import com.yuerlive.rainbow.module.mine.presenter.FeedbackContract;
import com.yuerlive.rainbow.module.mine.presenter.FeedbackPresenter;
import com.yuerlive.rainbow.widget.timeselector.TextUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by xiaoyi on 2017/5/31.
 */

public class FeedbackActivity extends BaseActivity implements FeedbackContract.View {
    @BindView(R.id.etContent)
    EditText etContent;
    @BindView(R.id.btnCommit)
    Button btnCommit;
    @BindView(R.id.tvKefu)
    TextView tvKefu;
    @BindView(R.id.tvFontNum)
    TextView tvFontNum;
    @BindView(R.id.llParent)
    LinearLayout llParent;

    FeedbackContract.Presenter presenter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_feedback;
    }

    @Override
    protected void initViews() {
        presenter = new FeedbackPresenter(this);
        setTitle("意见反馈");
        tvKefu.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                tvFontNum.setText(String.valueOf(400 - etContent.getText().length()));
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    long current;

    @OnClick({R.id.btnCommit, R.id.tvKefu})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnCommit:
                if (System.currentTimeMillis() - current < 5000) {
                    return;
                }
                current = System.currentTimeMillis();
                if (TextUtil.isEmpty(etContent.getText().toString())) {
                    showSnackbar(llParent, "请输入您想提交的内容",0);
                    return;
                }
                presenter.commitFeedback(etContent.getText().toString());
                break;
            case R.id.tvKefu:
                ClipboardManager clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                clipboardManager.setText(tvKefu.getText().toString().replace("客服QQ群：", ""));
                showSnackbar(llParent, "客服QQ群已复制到剪贴板",0);
                break;
        }
    }

    @Override
    public void setPresenter(FeedbackContract.Presenter presenter) {
        super.presenter = presenter;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }


    @Override
    public void showResult(int code) {
        if (code == 0) {
            showSnackbar(llParent, "提交成功，感谢您的反馈",0);
            llParent.postDelayed(()->finish(),2000);
        } else {
            showSnackbar(llParent, "提交失败，请稍后重试",0);
        }
    }
}
